package com.chunxiadong.service.impl;

import com.chunxiadong.entity.Blog;
import com.chunxiadong.entity.BlogBySearch;
import com.chunxiadong.entity.Type;
import com.chunxiadong.mapper.BlogMapper;
import com.chunxiadong.mapper.TypeMapper;
import com.chunxiadong.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @auther QiuShangcheng
 * @create 2021/6/6
 */
@Service
@SuppressWarnings("unchecked")
public class TypeServiceImpl implements TypeService {
    @Autowired
    TypeMapper typeMapper;

    @Autowired
    BlogMapper blogMapper;

    @Transactional
    @Override
    public int addType(Type type) {
        return typeMapper.addType(type);
    }

    @Transactional
    @Override
    public Type getType(Integer id) {
        return typeMapper.getType(id);
    }

    @Transactional
    @Override
    public int updateType(Type type) {
        return typeMapper.updateType(type);
    }

    @Transactional
    @Override
    public int deleteType(Integer id) {
        return typeMapper.deleteType(id);
    }


    @Transactional
    @Override
    public List<Type> getAllType() {
        List<Type> allType = typeMapper.getAllType();
        for (Type type : allType
        ) {
            List<Blog> blog = blogMapper.getBlogByTypeAndTitle(new BlogBySearch(type.getId(), null,true));
            type.setBlogList(blog);
        }
        return allType;
    }

    @Transactional
    @Override
    public PageInfo<Type> getAllTypeByPage(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<Type> allType = typeMapper.getAllType();
        PageInfo<Type> types = new PageInfo(allType);
        return types;
    }
}
